new Vue({
    el:'#main',
    data:()=>({
        type:'list',
        list:[],//数据
        cheallval:[],//勾选全选的值
        cheval:[],//被勾选的值
        isallche:false,//是否勾选全选
        lordrop:false,//升序或者降序
        pageSize:10,//分页条数
        page:1,//分页
        litreOrdrop:{
            editTime:false
        },
        auth_ids:$('#adminauth_ids').text()
    }),
    created(){
        if($('#formMenu').text() != 1){
            if(window.localStorage.getItem('page')){  
                this.page=window.localStorage.getItem('page')
            }
        }
        window.localStorage.removeItem('page')

        this.getData()
        $('.loding').hide()
    },
    methods:{
        formatUnit:formatUnit,
        sortFun(){
            this.list.sort((item1,item2)=>{
                if(this.litreOrdrop.editTime)
                return item1.create_at-item2.create_at
                else
                return item2.create_at-item1.create_at
            })
            this.litreOrdrop.editTime=!this.litreOrdrop.editTime
        },
        getData(){
            this.cheallval=[]//清空全选值
            this.isallche=false
            $.ajax({
                url:'/admin/Album/list?request='+this.type+'&page='+this.page+'&limit='+this.pageSize,
                type:'post',
                success:data=>{
                    data=JSON.parse(data)
                    if(data.data.pageSum > 0) {
                        this.page = data.data.page
                        this.Resetnav(this.page-0,data.data.pageSum)
                    }
                    data.data.data.forEach(element => {
                        this.cheallval.push(element.id)
                        element.ische=false
                    });
                    this.list=data.data.data
                    this.sortFun()
                    this.page=data.data.page
                }
            })
        },
        //切换Tab
        changeTab(type){
            this.type=type
            this.getData()
        },
        //点击全选
        cheall(){
            if(this.isallche)
            this.cheval=this.cheallval
            else
            this.cheval=[]
            this.list.forEach(element => {
                element.ische=this.isallche
            });
        },
        //勾选单个
        chaone(id,ische){
            if(ische){
                this.cheval.push(id)
            }else{
                this.cheval.splice(this.cheval.indexOf(id),1)
            }
            if(this.list.every(item=>item.ische))
            this.isallche=true
            else
            this.isallche=false
        },
        //点击排序
        sort(){
            this.list.sort((a,b)=>{
                if(this.lordrop)
                return 
                else
                return
            })
        },
        operation(ids,type){
            var url=""
            switch(type){
                //删除
                case 'del':
                    url='/admin/album/requestBatchDelete?ids='
                break;
                //恢复
                case 'restore':
                    url='/admin/album/requestRestore?id='
                break;
                //批量恢复
                case 'batchrestore':
                    url='/admin/album/requestBatchRestore?ids='
                break;
                //批量清除
                case 'batchclear':
                    url='/admin/album/requestBatchClear?ids='
                break;
                //清除
                case 'remove':
                    url='/admin/album/requestDel?id='
                break;
            }
            $.ajax({
                url:url+ids.join(','),
                type:'post',
                success:data=>{
                    data=JSON.parse(data)
                    change_mes_type('.alert',data.status)
                    if(data.status==1){
                        $('.alert').show()
                        $('.alert p').html('*'+data.msg+'<br>')
                        this.getData()
                    }else{
                        $('.alert').show()
                        $('.alert p').html('*'+data.msg+'<br>')
                    }
                }
            })
        },
        Resetnav(pageCurrent, pageSum) {
            //数据总条数
            var myPageCount = pageSum;
            //一页显示多少条
            var myPageSize = this.pageSize;
            //总页数
            var countindex = myPageCount % myPageSize > 0 ? (myPageCount / myPageSize) + 1 : (myPageCount / myPageSize);
            $.jqPaginator('#pagination', {
                totalPages: parseInt(countindex),
                //最多显示多少页
                visiblePages: 5,
                //默认显示第几页
                currentPage: pageCurrent,
                first: '<li class="first"><a href="javascript:;">'+langcon.page_home+'</a></li>',
                prev: '<li class="prev"><a href="javascript:;"><i class="arrow arrow2"></i>'+langcon.previous_page+'</a></li>',
                next: '<li class="next"><a href="javascript:;">'+langcon.next_page+'<i class="arrow arrow3"></i></a></li>',
                last: '<li class="last"><a href="javascript:;">'+langcon.last_page+'</a></li>',
                page: '<li class="page"><a href="javascript:;">{{page}}</a></li>',
                all:'<li class="page"><a href="javascript:;">'+myPageCount+' / '+this.page+'PG , '+this.pageSize+' /PG</a></li>',
                onPageChange:(num, type)=>{
                    if (type == "change") {
                        this.page=num
                    }
                }
            });
        },
        albumCopy(id){
            $.ajax({
                url:'/admin/album/albumCopy?id='+id,
                type:'post',
                success:data=>{
                    data=JSON.parse(data)
                    change_mes_type('.alert',data.status)
                    if(data.status==1){
                        $('.alert').show()
                        $('.alert p').html('*'+data.msg+'<br>')
                        this.getData()
                    }else{
                        $('.alert').show()
                        $('.alert p').html('*'+data.msg+'<br>')
                    }
                }
            })
        },
        setStorage(){
            var storage=window.localStorage;
            storage.setItem("page", this.page);
        },

    }
})